package org.dromara.common.core.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


public class WxMessage {

    /**
     * 获取ACCESSTOKEN
     * @param APPID
     * @param APPSECRET
     * @return
     */
    public static String getACCESSTOKEN(String APPID,String APPSECRET){
        String access_token = "";
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        //请求参数
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "GET","");
//        System.out.println("获取accessToken " + result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if(jsonObject!=null && jsonObject.size() > 0){
            access_token = jsonObject.containsKey("access_token") ? jsonObject.getStr("access_token"): "";
        }
        return access_token;
    }

    public static JSONObject getUserMpInfo(String ACCESS_TOKEN,String OPENID){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ACCESS_TOKEN+"&openid="+OPENID+"&lang=zh_CN";
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "GET","");
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }


    public static JSONObject getUserMp(String ACCESS_TOKEN,String NEXT_OPENID){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+ACCESS_TOKEN+"&next_openid="+NEXT_OPENID;
//        System.out.println("requestUrl:" + requestUrl);
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "GET","");
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }

    /**
     *  发送公众号推送消息
     * @param ACCESS_TOKEN
     * @param param
     * @return
     */
    public static JSONObject sendMsg(String ACCESS_TOKEN,String param){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ACCESS_TOKEN;
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "POST", param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }

    /**
     *  发送订阅消息
     * @param ACCESS_TOKEN
     * @param param
     * @return
     */
    public static JSONObject sendSubscribeMsg(String ACCESS_TOKEN,String param){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+ACCESS_TOKEN;
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "POST", param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }

    /**
     * 根据模板消息的 short_id  获取 模板的id
     * @param template_id_short
     * @return
     */
    public String getTemplateId(String template_id_short,String ACCESS_TOKEN){
        String template_id = "";

        String requestUrl = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token="+ACCESS_TOKEN;
        String param = "{\"template_id_short\":\""+template_id_short+"\"}";

        String result = WXPayCommonUtil.httpsRequest(requestUrl, "POST",param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if(jsonObject!=null && jsonObject.size() > 0){
            int errcode = jsonObject.getInt("errcode");
            String errmsg = jsonObject.getStr("errmsg");
            template_id = jsonObject.containsKey("template_id") ? jsonObject.getStr("template_id"): "";
        }
        return template_id;
    }


    /**
     * @description 校验内容是否违规
     * @author HanTao
     * @date 2022/7/13 14:42
     **/
    public static boolean checkContent(String openId, String content,String appid,String secret) throws IOException {
        String accessToken = getACCESSTOKEN(appid, secret);
        String url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("version", 2);
        jsonObject.set("openid", openId);
        jsonObject.set("scene", 3);
        byte[] ptext = content.getBytes();
        String value = new String(ptext, StandardCharsets.UTF_8);
        jsonObject.set("content", value);
        String ret = OkHttpClientUtil.getInstance().postJson(url, jsonObject.toString());
        JSONObject resultJson = JSONUtil.parseObj(ret);
        if (resultJson.getInt("errcode") == 0) {
            if (resultJson.getJSONObject("result") != null && resultJson.getJSONObject("result").containsKey("label")) {
                int label = resultJson.getJSONObject("result").getInt("label");
                if (label == 100) {
                    return false;
                }else {
                    return true;
                }
            }
        }
        return false;
    }



    public static void main(String args[]){

        String accessToken = getACCESSTOKEN("wx4023ebd185405f69","d2b3a8f37a51dd902316ecf0a0ac0711");
        String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;

//        System.out.println("accessToken:" + accessToken);
        JSONObject jsonObject = getUserMp(accessToken,"");
        if(jsonObject!=null){
            System.out.println(jsonObject.toString());
        }
    }
}
